package com.gperp.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gperp.common.GPResult;
import com.gperp.controller.LogAspect;
import com.gperp.mapper.AsLogsMapper;
import com.gperp.mapper.AsSystemconfigMapper;
import com.gperp.mapper.other.SystemConfigMapper2;
import com.gperp.pojo.AsLogs;
import com.gperp.pojo.AsLogsExample;
import com.gperp.pojo.AsSystemconfig;
import com.gperp.pojo.AsSystemconfigExample;
import com.gperp.pojo.Result;
import com.gperp.service.AsSystemconfigService;
@Service("asSystemconfigService")
public class AsSystemconfigServiceImpl implements AsSystemconfigService {
	@Resource
	private LogAspect aspect;
	@Autowired
	private  SystemConfigMapper2 systemConfigMapper;
	@Resource
	private AsSystemconfigMapper asSystemconfigMapper;
	@Resource
	private AsLogsMapper asLogsMapper;
	//财务查询所有
	public Result selectFinance(int page, int rows,int configtype) {
				// 设置分页信息
				PageHelper.startPage(page, rows);
				// 执行查询
				AsSystemconfigExample example=new AsSystemconfigExample();
				
				example.createCriteria().andConfigtypeEqualTo(configtype);
				List<AsSystemconfig> list = asSystemconfigMapper.selectByExample(example);
				// 取分页结果
				PageInfo<AsSystemconfig> pageInfo = new PageInfo<>(list);
				Result r = new Result();
				r.setRows(pageInfo.getList());
				r.setPageIndex(page);
				r.setTotal(pageInfo.getTotal());		
				return r;
		}
	


	@Override//财务添加
	public GPResult insertFinance(String configtypename,int isstart,int configType) {
		try {
			AsSystemconfig config=new AsSystemconfig();
			config.setConfigtypename(configtypename);
			config.setConfigtype(configType);
			config.setIsstart(isstart);
			config.setConfigtypevalue(systemConfigMapper.getMaxConfigType(configType)+1);
			config.setConfigvalue("test");
			asSystemconfigMapper.insertSelective(config);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}return GPResult.ok();
	}


	@Override
	public AsSystemconfig selectByPrimaryKey(Long id) {
		return asSystemconfigMapper.selectByPrimaryKey(id);
	}


	@Override
	public int updateByPrimaryKey2(AsSystemconfig record) {
		return systemConfigMapper.updateByPrimaryKey2(record);
	}



	@Override
	public int deleteByPrimaryKey(Long id) {
		return asSystemconfigMapper.deleteByPrimaryKey(id);
	}

	//服务网添加
	public GPResult insertService(String configtypename,int isstart,String configvalue,int configType) {
         try {
			AsSystemconfig config=new AsSystemconfig();
			config.setConfigtype(configType);
			config.setConfigtypename(configtypename);
			config.setConfigtypevalue(systemConfigMapper.getMaxConfigType(configType)+1);
			config.setConfigvalue(configvalue);
			config.setIsstart(isstart);
			System.out.println("dfsfsds"+config.getConfigtype());
			asSystemconfigMapper.insertSelective(config);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}return GPResult.ok();
	}


	@Override
	public int updateService(AsSystemconfig record) {
		return systemConfigMapper.updateService(record);
	}

	public Result selectService(int page, int rows, int configtype) {
				// 设置分页信息
				PageHelper.startPage(page, rows);
				// 执行查询
				AsSystemconfigExample example=new AsSystemconfigExample();
				example.createCriteria().andConfigtypeEqualTo(configtype);
				List<AsSystemconfig> list = asSystemconfigMapper.selectByExample(example);
				// 取分页结果
				PageInfo<AsSystemconfig> pageInfo = new PageInfo<>(list);
				Result r = new Result();
				r.setRows(pageInfo.getList());
				r.setPageIndex(page);
				r.setTotal(pageInfo.getTotal());		
				return r;
		}


	@Override
	public GPResult updateClient(String configtypename, int isstart,
			int configType,Long id) {
		try {
			AsSystemconfig config=new AsSystemconfig();		
			config.setConfigtypename(configtypename);
			config.setIsstart(isstart);
			config.setConfigtype(configType);
			config.setId(id);
			config.setConfigvalue("");
			asSystemconfigMapper.updateByPrimaryKeySelective(config);
		} catch (Exception e) {
			e.printStackTrace();
			return GPResult.build(200, "NO");
		}
		return GPResult.ok();
	}



	@Override
	public AsSystemconfig selectApp(AsSystemconfigExample example) {
		return systemConfigMapper.selectApp(example);
	}



	@Override
	public int updateApp(AsSystemconfig record) {
		return systemConfigMapper.updateApp(record);
	}



	@Override
	public AsSystemconfig selectServiceYear(AsSystemconfigExample example) {
		return systemConfigMapper.selectServiceYear(example);
	}



	@Override
	public int updateServiceYear(AsSystemconfig record) {
		return systemConfigMapper.updateServiceYear(record);
	}



	  public Result selectLogs(int page, int rows) {
		// 设置分页信息
		PageHelper.startPage(page, rows);
		// 执行查询
		AsLogsExample example=new AsLogsExample();
		//example.createCriteria().andUsernameEqualTo(aspect.getUser().getUsercode());
		example.setOrderByClause("Operatedatetime desc");
		List<AsLogs> list = asLogsMapper.selectByExample(example);
		// 取分页结果
		PageInfo<AsLogs> pageInfo = new PageInfo<>(list);
		Result r = new Result();
		r.setRows(pageInfo.getList());
		r.setPageIndex(page);
		r.setTotal(pageInfo.getTotal());		
		return r;
	}


	
}
